﻿package cn.poco.pepsi.activity.detail;



import cn.poco.pepsi.R;
import cn.poco.pepsi.activity.discut.DiscutDetailActivity;
import cn.poco.pepsi.activity.publish.PublishedMainActivity;
import cn.poco.pepsi.bean.ResDetail;
import cn.poco.pepsi.bean.Restaurant;
import cn.poco.pepsi.service.ISearch;
import cn.poco.pepsi.service.impl.SearchImpl;
import cn.poco.pepsi.util.Cons;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.HandlerThread;
import android.os.Message;
import android.text.Html;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;

public class ResDetailActivity_x extends Activity {

	/** constants **/
	

	private Handler tHandler;
	private ResDetail res=null;
	private String resid = null;
	Bundle bundel = null;
	//               标题       菜式      周边            地址             电话      主要菜式     
	private TextView title, 
	                                  dish, 
	                                 circle, 
	                                 discut,
	                                 address,
	                                 tel, 
	                                 mainDish,
	                                 intro, linediscut,
	                                 ucomment,
	                                 recomment, 
	                                 average, 
	                                 textdiscount;
	private ImageView imageview, join, pepsi, discount, star;
	private LinearLayout laydiscount;

	/** 获取餐厅详细信息 **/
	private ResDetail resdetail;
	private ProgressBar progress_small, imageProgress;

private ImageButton onhere=null;
private ImageButton comment=null;
	
	protected void initView(){
		star = (ImageView) findViewById(R.id.star_ps);
		average = (TextView) findViewById(R.id.average_ps);
		dish = (TextView) findViewById(R.id.dish);
		circle = (TextView) findViewById(R.id.circle);
		laydiscount = (LinearLayout) findViewById(R.id.laydiscount);
		discut=(TextView) findViewById(R.id.into_discut_text);
		
		address = (TextView) findViewById(R.id.address_ps);
		tel = (TextView) findViewById(R.id.tel_ps);
		mainDish = (TextView) findViewById(R.id.main_dish_ps);
		intro = (TextView) findViewById(R.id.intro_ps);
		linediscut=(TextView) findViewById(R.id.line_discut);
		
		onhere=(ImageButton) findViewById(R.id.onhere);
		comment=(ImageButton) findViewById(R.id.comment_ps);
		
		
	}
	
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.res_detail_ps);
		initView();
		bundel=getIntent().getExtras();
		res=(ResDetail) bundel.get(Cons.RESDATA);
		resid= bundel.getString(Cons.RES_CODE);//接收来自启动它的Activity的餐厅ID
		
		HandlerThread ht = new HandlerThread("XW");
		ht.start();
		tHandler = new Handler(ht.getLooper());
		tHandler.post(runnable);//启动获取餐厅详情的线程
		
		
		discut.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				Intent intent=new Intent(ResDetailActivity_x.this,DiscutDetailActivity.class);
				intent.putExtra(Cons.RES_CODE, resid);
				System.out.println("res.getId()--------->"+resid);
				startActivity(intent);
			}
		});
		
		
		
		comment.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				
				if(true){//判断是否已登录
					Intent intent=new Intent(ResDetailActivity_x.this,PublishedMainActivity.class);
					startActivity(intent);
					
				}
				else{
					
				}
			}
		});
		
		
		
	
		

			
		}


Runnable runnable=new Runnable() {
		
		@Override
		public void run() {
			try {
				if(res!=null){
					handler.sendEmptyMessage(1);//更新UI
				}
			} catch (Exception e) {
				// TODO: handle exception
			}
		}};
	
		Handler handler = new Handler() {
			public void handleMessage(Message msg) {
				switch (msg.what) {
				case 1:
					   UpdateUI();
					break;
				}
			}
		};
		


	// UPDATE UI
	private void UpdateUI() {
		//设置星级
		whichstar();
		//人均
		average.setText("人均："+res.getAverage());
		dish.setText(" 菜式："+res.getDish());
		circle.setText("地区："+res.getArea());
		address.setText(res.getAddress());
		tel.setText(res.getTel());
		isDiscut();
		mainDish.setText(res.getMaindish());
		if(!res.getRecomment().equals("")){
			intro.setText(res.getRecomment());
		}
		
		

		
		}

	
	//设置优惠优信息
	private void isDiscut(){
		if(res.getDiscount()==1){
			laydiscount.setVisibility(View.VISIBLE);
			linediscut.setVisibility(View.VISIBLE);
			discut.setText(Html.fromHtml("<u>" + "点击查看优惠详情" + "</u>"));
		}
	}
	
	
	//设置星级
	
	private void whichstar(){
		int n=Integer.parseInt(res.getStar());
		switch (n) {
		    case 1:
		    	star.setImageResource(R.drawable.x1);
			break;
            case 2:
            	star.setImageResource(R.drawable.x2);
			break;
			case 3:
				star.setImageResource(R.drawable.x3);
			break;
			case 4:
				star.setImageResource(R.drawable.x4);
			break;
			case 5:
				star.setImageResource(R.drawable.x5);
			break;
		default:
			star.setImageResource(R.drawable.x0);
			break;
		}
		
		
		
	}
	
	


	

	




	

}
